/*
 * Copyright 2007-2010 Jeremy Brown.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author <a href="http://jeremy-brown.com">Jeremy Brown</a>
 */

//Demonstrate creating a Prefuse table from scratch using attributes in the the builder method "table", and the "row" builder method
package groovy.prefuse.demos

import groovy.swing.*
import groovy.prefuse.*
import groovy.prefuse.factory.*

import javax.swing.*

import prefuse.*
import prefuse.action.*
import prefuse.action.assignment.*
import prefuse.action.layout.*
import prefuse.action.layout.graph.*
import prefuse.activity.*
import prefuse.controls.*
import prefuse.data.*
import prefuse.render.*
import prefuse.util.*
import prefuse.visual.*
import prefuse.visual.expression.*

def prefuse = new PrefuseBuilder()

def group = "data"
def xfield = "city"
def yfield = "price-in-FCFA"
def sfield = "drink"

def frame = prefuse.frame(title:'Table Demo 1', preferredSize:[750, 550], defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE)
{
	pfDisplay(id:"display", size:[720, 500], highQuality:true, border:BorderFactory.createEmptyBorder(10,10,10,10))
	{
		pfVisualization(id:"vis")
		{
			pfTable(id:"drinks", group:group,
					columnNames:["drink", "city", "price-in-FCFA"], 
					columnTypes:[String, String, Integer], 
					columnDefaults:["", "", 0])
			{
				pfRow(["Coca-cola", "Brazzaville", 800])
				pfRow(["Fanta", "Brazzaville", 700])
				pfRow(["Djino", "Brazzaville", 2000])
				pfRow(["Coca-cola", "Ouesso", 500])
				pfRow(["Fanta", "Ouesso", 600])
				pfRow(["Djino", "Ouesso", 700])
				pfRow(["Coca-cola", "Pointe Noire", 400])
				pfRow(["Fanta", "Pointe Noire", 500])
				pfRow(["Djino", "Pointe Noire", 1000])
			}

			pfRenderer(ShapeRenderer, baseSize:10)
			pfLayout(AxisLayout, id:"x_axis", name:"x", group:group, dataField:xfield, axis:Constants.X_AXIS, filter:VisiblePredicate.TRUE)
			pfLayout(layout:AxisLayout, id:"y_axis", name:"y", group:group, dataField:yfield, axis:Constants.Y_AXIS, filter:VisiblePredicate.TRUE)
			pfAssignment(ColorAction, id:"color", name:"color", group:group, field:VisualItem.STROKECOLOR, defaultColor:ColorLib.rgb(100,100,100))
			{
				//the following two commands are the same. colorAction(...) is a shortcut for assignment(ColorAction, ...)
				pfColorAction(predicate:"[city]='Brazzaville'", id:"colorB", name:"colorB", group:group, field:VisualItem.STROKECOLOR, defaultColor:ColorLib.rgb(255,150,150))
				pfAssignment(ColorAction, predicate:"[city]='Ouesso'", id:"colorO", name:"colorO", group:group, field:VisualItem.STROKECOLOR, defaultColor:ColorLib.rgb(50,255,50))
				pfColorAction(predicate:"[city]='Pointe Noire'", id:"colorP", name:"colorP", group:group, field:VisualItem.STROKECOLOR, defaultColor:[50,50,255])
			}
			pfAssignment(DataShapeAction, id:"shape", name:"shape", group:group, dataField:sfield)

			//Either create the actionList like this:
			pfActionList(id:"draw", name:"draw")
			{
				pfLayout(x_axis)
				pfLayout(y_axis)
				if(sfield != null){ pfAction(shape) }
				pfAction(color)
				pfAction(new RepaintAction())
			}
			
			//Or create the actionList like this:
			//actionList(actionList:[x_axis, y_axis, shape, color, new RepaintAction()], id:"draw", name:"draw")
		}

		//ToolTipControl causes a tooltip to be displayed, with data from the specified fields, when the mouse hovers over the data point for at least a second or two
		pfControl(ToolTipControl, fields:[sfield,xfield,yfield])
	}
}

frame.show()
frame.pack()
prefuse.vis.run("draw");

